package com.springboot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.common.Result;
import com.springboot.entity.Product;
import com.springboot.service.ProductService;
import com.springboot.vo.SearchProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "奶茶店后台商品管理",value = "商品管理")
@RestController
@RequestMapping("/shop")
@CrossOrigin
public class ProductController {
    @Autowired
    ProductService productService;
    /**
     * 添加商品;
     * @return  是否添加成功;
     */
    @PostMapping("/AddProduct")
    @ApiOperation(value = "商品上架",httpMethod = "POST")
    public Result AddProduct(Product product){
        Product product1=productService.LookUpProduct(product.getSkuId());
        if(product1!=null){
            return Result.fail("添加失败，已经有该商品",null);
        }else {
            Integer i=productService.AddProduct(product);
            return Result.success("添加成功",i);
        }
    }

    /**
     * 删除商品;
     * @param skuId  通过商品编号去删除;
     * @return 返回是否删除成功;
     */
    @DeleteMapping("/delete/product")
    @ApiOperation(value = "删除商品",httpMethod = "DELETE")
    public Result DeleteProduct(@RequestParam("skuId") Integer skuId){
        Integer integer= productService.DeleteProduct(skuId);
        return Result.success("删除成功",integer);
    }

    /**
     * 查找商品;
     * @param searchProductVo  多条件查询商品;
     * @return 该商品;
     */
    @GetMapping("/selectProduct/byName")
    @ApiOperation(value = "模糊条件查询商品",httpMethod = "GET")
    public Result LookProductByName(SearchProductVo searchProductVo){
        System.out.println(searchProductVo);
        if(searchProductVo.getPageNum()!=null||searchProductVo.getPageSize()!=null){
            PageHelper.startPage(searchProductVo.getPageNum(),searchProductVo.getPageSize());
            List<Product> productList= productService.LookProductManyCondition(searchProductVo);
            PageInfo<Product> pageInfo=new PageInfo<>(productList);
            return Result.success("查询成功",pageInfo);
        }else {
            List<Product> productList= productService.LookProductManyCondition(searchProductVo);
            return Result.success("查询成功",productList);
        }
    }

    /**
     * 修改商品的信息;
     * @param product  修改后的商品信息;
     * @return  返回是否修改成功
     */
    @PostMapping("/update/product")
    @ApiOperation(value = "修改商品的信息",httpMethod = "POST")
    public Result UpdateProduct(@RequestBody Product product){
        Integer integer=productService.UpdateProduct(product);
        return Result.success("成功修改商品信息",integer);
    }
}
